import { NavBar, usePageTitle } from '@cscec/udp-mobile-ui';

export const isUseDefaultPageTitle = (pageTitle) => !pageTitle || pageTitle === '默认页面标题';
export function PageTitle({ listTitle }) {
  const { showNav, pageTitle } = usePageTitle(listTitle);
  return showNav ? <NavBar>{pageTitle}</NavBar> : null;
}
export const generateButtons = (
  buttons = [] as any[],
  type,
  { callback, disableBtn = [] as any[], hideBtn = [] as any[] }
) =>
  buttons
    .map(({ danger, id, type: btnType = 'default', color, size = 'middle', beforeClick, afterClick, ...rest }, i) => ({
      ...rest,
      disabled: disableBtn.includes(id),
      hidden: hideBtn.includes(id),
      type: danger ? 'danger' : btnType,
      key: id ?? `${type}_btn_${i}`,
      size,
      onClick: async (data?: any) => {
        beforeClick && new Function(beforeClick)();
        await callback(type, id, data).catch((err) => {
          console.log('未处理异常', err);
        });
        afterClick && new Function(afterClick)();
      }
    }))
    .filter((button) => !button.hidden);